package dao

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/helper/resphelper/placeresp"
	"yundian/internel/app/models/comment"
	"yundian/internel/app/models/membermodel"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/usersteward"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
)

type NotificationDao struct {
}

func NewNotificationDaoDao() *NotificationDao {
	return &NotificationDao{}
}

func (dao *NotificationDao) GetCommentSubmitNum(memberId, hour int) *dingo.ErrorWrap {
	var num int64

	db := global.Orm.Table("third_comment tc")
	db.Where("tc.member_id=? AND DATE_SUB(NOW(), interval ? HOUR) < create_time", memberId, hour)
	db.Count(&num)

	return dingo.ErrorIf(db.Error, num)
}

// GetStewardServiceAutomateReplenishList 补货服务数据列表
func (dao *NotificationDao) GetStewardServiceAutomateReplenishList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.ServiceAutomateReplenishListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.device_sn,psn.status,psn.id,psn.info,psn.place_room,psn.user_id,psn.place_id,p.place_name,u.user_name,u.phone,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users u ON u.id=p.user_id")
	db.Where("psn.type=13")

	if !helper.IsAdminAndStaff(param.IsAdmin, param.ParentId) {
		userIdArr := NewUserDao().QueryChildUserIds(param.LoginUserId).Unwrap().([]int)
		placesIdArr := NewUserDao().GetPlacesIds(param.LoginUserId, userIdArr).Unwrap().([]int)
		db.Where("p.id IN ?", placesIdArr)
	}

	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.Status == 6 {
		db.Where("psn.status=?", param.Status)
	}
	if param.Status == 1 {
		db.Where("psn.status<>?", 6)
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetStewardServiceAutomateReplenishDataList 补货服务数据列表
func (dao *NotificationDao) GetStewardServiceAutomateReplenishDataList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.ServiceAutomateReplenishDataListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.user_id,psn.place_id,COUNT(1)num,SUM(IF(psn.status=6,1,0))r_num,
	SUM(IF(psn.status<>6,1,0))n_num,p.place_name,u.user_name,u.phone`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users u ON u.id=p.user_id")
	db.Where("psn.type=13")

	if !helper.IsAdminAndStaff(param.IsAdmin, param.ParentId) {
		userIdArr := NewUserDao().QueryChildUserIds(param.LoginUserId).Unwrap().([]int)
		placesIdArr := NewUserDao().GetPlacesIds(param.LoginUserId, userIdArr).Unwrap().([]int)
		db.Where("p.id IN ?", placesIdArr)
	}

	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.UserName != "" {
		db.Where("u.user_name LIKE ?", "%"+param.UserName+"%")
	}
	if param.Phone != "" {
		db.Where("u.phone LIKE ?", "%"+param.Phone+"%")
	}

	db.Group("psn.place_id").Find(&lst)
	//db.Limit(limit).Offset(offset).Find(&lst)
	count = int64(len(lst))
	lst = lst[offset:utils.IfTrueInt(offset+limit > int(count), int(count), offset+limit)]
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetAppStaffInformMessageList 查询员工通知消息
func (dao *NotificationDao) GetAppStaffInformMessageList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	staffData, types, _ := dao.GetStaffInfoById(param.LoginUserId)
	if staffData.Id == 0 {
		return dingo.ErrorIf(nil, lst, count)
	}

	fields := `psn.id, member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %T')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %T')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places_staff ps ON psn.place_id=ps.place_id")
	db.Where("ps.staff_id=? AND ps.status in (?)", param.LoginUserId, []int{1, 2})

	if param.PlaceId > 0 {
		db.Where("psn.place_id = ?", param.PlaceId)
	}
	if len(types) > 0 {
		db.Where("psn.type IN ?", types)
	}
	if param.Status > 4 {
		db.Where("psn.status=?", param.Status)
	}
	if param.Status <= 4 && param.Status > 0 {
		db.Where("psn.status<=?", param.Status)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

func (dao *NotificationDao) StewardStatisticsMaintain(param *request.StewardStatisticsReq) *dingo.ErrorWrap {
	var targetList []*request.StewardStatisticsNameRes
	data := dao.StewardStatisticsInfo(param)
	lst := data.Unwrap().([]*request.StewardStatisticsRes)
	keyMap := map[string]int{}
	for _, v := range lst {
		for _, str := range strings.Split(v.Info, "、") {
			keyMap[str] += v.Num
		}
	}
	for name, num := range keyMap {
		targetList = append(targetList, &request.StewardStatisticsNameRes{Name: name, Num: num})
	}
	return dingo.ErrorIf(data.Err, targetList)
}

func (dao *NotificationDao) StewardStatisticsDaily(param *request.StewardStatisticsReq) *dingo.ErrorWrap {
	var targetList []*request.StewardStatisticsNameRes
	data := dao.StewardStatisticsInfo(param)
	lst := data.Unwrap().([]*request.StewardStatisticsRes)
	keyMap := map[string]int{}
	for _, v := range lst {
		for _, str := range strings.Split(v.Info, "、") {
			if strings.Index(str, "件") == -1 {
				continue
			}
			tLst := strings.Split(str, "-")
			if len(tLst) != 2 {
				continue
			}
			n, _ := strconv.Atoi(utils.RegexpMatch(`[1-9]{1}[0-9]{0,5}`, tLst[1])[0])
			keyMap[tLst[0]] += v.Num * n
		}
	}
	for name, num := range keyMap {
		targetList = append(targetList, &request.StewardStatisticsNameRes{Name: name, Num: num})
	}
	return dingo.ErrorIf(data.Err, targetList)
}

func (dao *NotificationDao) StewardStatisticsInfo(param *request.StewardStatisticsReq) *dingo.ErrorWrap {
	var lst []*request.StewardStatisticsRes

	db := global.Orm.Table("places_service_notification psn").Select("type,info,COUNT(1)num")
	db.Joins("LEFT JOIN places p ON psn.place_id=p.id").Where("psn.type=?", param.Type)

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.StartTime != "" {
		db.Where("psn.create_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.create_time<=?", param.EndTime)
	}

	db.Group("info").Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (dao *NotificationDao) StewardStatisticsService(param *request.StewardStatisticsReq) *dingo.ErrorWrap {
	var lst []*request.StewardStatisticsRes

	db := global.Orm.Table("places_service_notification psn").Select("type,COUNT(1)num")
	db.Joins("LEFT JOIN places p ON psn.place_id=p.id")

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.StartTime != "" {
		db.Where("psn.create_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.create_time<=?", param.EndTime)
	}

	db.Group("type").Order("num DESC").Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (dao *NotificationDao) StewardStatisticsServiceDays(param *request.StewardStatisticsReq) *dingo.ErrorWrap {
	var lst []*request.StewardStatisticsDaysRes

	fields := `DATE_FORMAT(create_time,'%Y-%m-%d') day,COUNT(1)num,type`
	db := global.Orm.Table("places_service_notification psn").Select(fields)

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		db.Joins("LEFT JOIN places p ON psn.place_id=p.id")
		db.Where("p.user_id=?", param.LoginUserId)
	}

	//if param.Type > 0 {
	db.Where("psn.type=?", param.Type)
	//}
	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.StartTime != "" {
		db.Where("psn.create_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.create_time<=?", param.EndTime)
	}

	db.Group("type,day").Order("day").Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (dao *NotificationDao) DeletePlacesServiceMaterials(param *request.DbPlacesServiceMaterialsListReq) *dingo.ErrorWrap {
	db := global.Orm.Model(&param.DB).Delete("id", param.DB.Id)
	return dingo.ErrorIf(db.Error)
}

func (dao *NotificationDao) InsertPlacesServiceMaterials(param *request.DbPlacesServiceMaterialsListReq) *dingo.ErrorWrap {
	db := global.Orm.Create(&param.DB)
	return dingo.ErrorIf(db.Error)
}

func (dao *NotificationDao) UpdatePlacesServiceMaterials(param *request.DbPlacesServiceMaterialsListReq) *dingo.ErrorWrap {
	db := global.Orm.Where("id=?", param.DB.Id).Updates(&param.DB)
	return dingo.ErrorIf(db.Error)
}

func (dao *NotificationDao) GetAdminPlacesServiceMaterialsList(param *request.PlacesServiceMaterialsListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.PlacesServiceMaterialsListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	field := `psm.id,place_id,psm.status,type,name,message,p.place_name,DATE_FORMAT
	(create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("places_service_materials psm").Select(field)
	db.Joins("LEFT JOIN places p ON psm.place_id=p.id")

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		uList := NewUserDao().QueryChildUserIds(param.LoginUserId).Unwrap().([]int)
		pList := NewUserDao().GetPlacesIds(param.LoginUserId, uList).Unwrap().([]int)
		db.Where("p.id IN ?", pList)
		//db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 0 {
		db.Where("psm.status=?", param.Status)
	}
	if param.Type > 0 {
		db.Where("psm.type=?", param.Type)
	}
	if param.PlaceId > 0 {
		db.Where("psm.place_id=?", param.PlaceId)
	}
	if param.Name != "" {
		db.Where("psm.name LIKE ?", "%"+param.Name+"%")
	}

	db.Order("psm.id desc")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)

	return dingo.ErrorIf(db.Error, lst, count)
}

// GetPlacesServiceMaterialsList 查询服务物品列表
func (dao *NotificationDao) GetPlacesServiceMaterialsList(param *request.WxPlacesServiceMaterialsListReq) *dingo.ErrorWrap {
	var lst []*placemodel.PlacesServiceMaterialsDb

	db := global.Orm.Where("place_id=? AND type=? AND status=?", param.PlaceId, param.Type, helper.TypeNumIsDrawOpen)
	db.Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// 根据通知ID查询通知详情
func (dao *NotificationDao) GetNotificationServiceInfoById(id int) *dingo.ErrorWrap {
	dataInfo := &placemodel.PlacesServiceNotification{}

	db := global.Orm.Table("places_service_notification").Where("id = ?", id).Find(&dataInfo)
	return dingo.ErrorIf(db.Error, dataInfo)
}

// 查询员工信息
func (dao *NotificationDao) GetStaffInfoById(staffId int) (dbData *usersteward.StUserStaff, types []int, err error) {
	dbData = &usersteward.StUserStaff{}
	db := global.Orm.Where("id=? AND status=1", staffId).Find(&dbData)
	if dbData != nil {
		_ = json.Unmarshal([]byte(dbData.MessageTypes), &types)
	}

	return dbData, types, db.Error
}

// 查询员工信息
func (dao *NotificationDao) GetStaffOpenIdByPlaceId(placeId int, nType uint8) *dingo.ErrorWrap {
	var lst []placeresp.StaffBasicInfo
	var nLst []placeresp.StaffBasicInfo
	sql := `select us.tencent_openid,us.name,us.id,us.phone,us.ding_talk_user_id,us.group_wechat_user_id,us.workforce_config, 
	us.message_types from places_staff ps left join users_staff us ON us.id = ps.staff_id where ps.status=1 and ps.place_id=?`
	db := global.Orm.Raw(sql, placeId).Group("us.id").Find(&lst)

	// 按配置类型查询
	for _, item := range lst {
		var nTypeList []uint8
		_ = json.Unmarshal([]byte(item.MessageTypes), &nTypeList)
		if len(nTypeList) == 0 {
			nLst = append(nLst, item)
			continue
		}
		for _, ty := range nTypeList {
			if ty == nType {
				nLst = append(nLst, item)
				break
			}
		}
	}

	return dingo.ErrorIf(db.Error, nLst)
}

func (dao *NotificationDao) InsertPlacesServiceRecord(m *placemodel.PlacesServiceRecord) *dingo.ErrorWrap {
	db := global.Orm.Table("places_service_record").Create(&m)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *NotificationDao) NotificationServiceInsert(m *placemodel.PlacesServiceNotification) *dingo.ErrorWrap {
	db := global.Orm.Table("places_service_notification").Create(&m)
	return dingo.ErrorIf(db.Error, m)
}

func (dao *NotificationDao) GetPlacesNotificationConfig(placeId int) *dingo.ErrorWrap {
	m := &placemodel.PlacesInformationConfig{}
	sql := `select * from places_information_config where place_id = ? and status = 1`
	db := global.Orm.Raw(sql, placeId).Find(m)
	return dingo.ErrorIf(db.Error, m)

}

func (dao *NotificationDao) UpdatePlaceStaffUserId(m *usersteward.StUserStaff, phone string) {
	global.Orm.Table("users_staff").Where("phone = ?", phone).Model(&m).Updates(&m)
}

func (dao *NotificationDao) UpdatePlaceServiceNotification(m *placemodel.PlacesServiceNotification) {
	global.Orm.Table("places_service_notification").Where("id = ?", m.Id).Model(&m).Updates(&m)
}

//查询会员信息
func (dao *NotificationDao) GetMemberByUserId(userId int) *dingo.ErrorWrap {
	var isUser int64
	db := global.Orm.Table("members").Where("id = ?", userId).Find(&isUser)
	return dingo.ErrorIf(db.Error, isUser)
}

//func (dao *NotificationDao) UpdatePlaceStaffGroupWeChatUserId(groupWeChatUserId, phone string) {
//	m := &usersteward.StUserStaff{}
//	m.DingTalkUserId = groupWeChatUserId
//	global.Orm.Table("users_staff").Where("phone = ?", phone).Model(m).Updates(m)
//}

// GetAppNotificationServiceInfo 查询通知
func (dao *NotificationDao) GetAppNotificationServiceInfo(param *request.StewardAppGetServiceNumReq) *dingo.ErrorWrap {
	var dataNumInfo []*request.StewardAppGetServiceNumRes
	if param.Status == 0 {
		return dingo.ErrorIf(nil, dataNumInfo)
	}

	fields := `psn.type,COUNT(1)num`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places_staff ps ON psn.place_id = ps.place_id")
	db.Where("ps.status IN (1,2) AND staff_id=?", param.LoginUserId)
	db.Where("psn.place_id=?", param.PlaceId)

	if len(param.MessageTypes) > 0 {
		db.Where("psn.type IN ? OR psn.user_id=?", param.MessageTypes, param.LoginUserId)
	}
	if param.Status >= 5 {
		db.Where("psn.status=? AND psn.user_id=?", param.Status, param.LoginUserId)
	} else {
		db.Where("psn.status<=?", 4)
	}

	db.Group("psn.type").Find(&dataNumInfo)
	return dingo.ErrorIf(db.Error, dataNumInfo)
}

func (dao *NotificationDao) GetAppNotificationServiceInfoTotal(param *request.StewardAppGetServiceNumReq) *dingo.ErrorWrap {
	dataNumInfo := &request.StewardAppGetServiceNumRes{}
	if param.Status == 0 {
		return dingo.ErrorIf(nil, dataNumInfo)
	}

	fields := `psn.type,COUNT(1)num`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places_staff ps ON psn.place_id = ps.place_id")
	db.Where("ps.status IN (1,2) AND staff_id=?", param.LoginUserId)
	db.Where("psn.place_id=?", param.PlaceId)

	if len(param.MessageTypes) > 0 {
		db.Where("psn.type IN ? OR psn.user_id=?", param.MessageTypes, param.LoginUserId)
	}
	if param.Status >= 5 {
		db.Where("psn.status=? AND psn.user_id=?", param.Status, param.LoginUserId)
	} else {
		db.Where("psn.status<=?", 4)
	}

	db.Find(&dataNumInfo)
	return dingo.ErrorIf(db.Error, dataNumInfo)
}

// GetNotificationServiceStatusInfo 查询通知状态数量
func (dao *NotificationDao) GetNotificationServiceStatusInfo(param *request.NotificationListReq) *dingo.ErrorWrap {
	dataNumInfo := &request.NotificationStatusInfoRes{}

	fields := `SUM(IF(psn.status<=4,1,0))pending,SUM(IF(psn.status=5,1,0))progress,
	SUM(IF(psn.status=6,1,0))done,SUM(IF(psn.status=7,1,0))cancel`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}

	db.Find(&dataNumInfo)
	return dingo.ErrorIf(db.Error, dataNumInfo)
}

// GetNotificationServiceInfo 查询通知
func (dao *NotificationDao) GetNotificationServiceInfo(param *request.NotificationListReq) *dingo.ErrorWrap {
	dataNumInfo := &request.NotificationInfoRes{}

	fields := `SUM(IF(psn.type=1, 1, 0))sweep,SUM(IF(psn.type=2, 1, 0))daily,SUM(IF(psn.type=3, 1, 0))maintain,
	SUM(IF(psn.type=4, 1, 0))to_call,SUM(IF(psn.type=5, 1, 0))invoice,SUM(IF(psn.type=6, 1, 0))continues,
	SUM(IF(psn.type=7, 1, 0))check_out,SUM(IF(psn.type=9, 1, 0))comment,SUM(IF(psn.type=11, 1, 0))supermarket,
	SUM(IF(psn.type=12, 1, 0))wake`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")

	if !helper.IsAdminAndStaff(param.IsAdmin, param.ParentId) {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}

	db.Find(&dataNumInfo)
	return dingo.ErrorIf(db.Error, dataNumInfo)
}

// GetNotificationServiceList 查询通知
func (dao *NotificationDao) GetNotificationServiceList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.id, member_id, place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info,us.name, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %k:%i')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %k:%i')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %k:%i')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %k:%i')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")

	if !helper.IsAdminAndStaff(param.IsAdmin, param.ParentId) {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if len(param.TypeList) > 0 {
		db.Where("psn.type IN ?", param.TypeList)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}
	if param.StaffId > 0 {
		db.Where("psn.user_id=?", param.StaffId)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.Room != "" {
		db.Where("psn.place_room LIKE ?", fmt.Sprintf(`%s%s%s`, "%", param.Room, "%"))
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetNotificationServiceRoomList 查询通知-房间续住
func (dao *NotificationDao) GetNotificationServiceRoomList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationRoomListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.id, psn.member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info,us.name, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %k:%i')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %k:%i')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %k:%i')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %k:%i')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")

	if !helper.IsAdminAndStaff(param.IsAdmin, param.ParentId) {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}
	if param.StaffId > 0 {
		db.Where("psn.user_id=?", param.StaffId)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.Room != "" {
		db.Where("psn.place_room LIKE ?", fmt.Sprintf(`%s%s%s`, "%", param.Room, "%"))
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetNotificationServiceGoodsList 查询通知-客房超市
func (dao *NotificationDao) GetNotificationServiceGoodsList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationGoodsListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.id, psn.member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info,us.name, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %k:%i')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %k:%i')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %k:%i')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %k:%i')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")

	if b := helper.IsAdminAndStaff(param.IsAdmin, param.ParentId); !b {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}
	if param.StaffId > 0 {
		db.Where("psn.user_id=?", param.StaffId)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.Room != "" {
		db.Where("psn.place_room LIKE ?", fmt.Sprintf(`%s%s%s`, "%", param.Room, "%"))
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GrantServiceCommentAward 好评有礼-发放奖励
func (dao *NotificationDao) GrantServiceCommentAward(param *request.GrantServiceCommentAwardReq) *dingo.ErrorWrap {
	dbInfo := &comment.SendRedPacketRecordDb{}
	fields := `go.user_id,go.member_id,go.place_id,go.id order_id,go.price money,
	m.xcx_openid,CONCAT(go.id,DATE_FORMAT(NOW(),'%Y%m%d'))mch_send_id`
	db := global.Orm.Table("third_comment tc").Select(fields)
	db.Joins("LEFT JOIN goods_orders go ON go.id=tc.goods_order_id")
	db.Joins("LEFT JOIN members m ON m.id=go.member_id")
	db.Where("tc.id=?", param.CommentId)
	db.Find(&dbInfo)
	if db.Error != nil {
		return dingo.ErrorIf(db.Error)
	}

	dbInfo.Status = helper.TypeNumRedPacketStatusNotAlready
	db = global.Orm.Create(&dbInfo)
	return dingo.ErrorIf(db.Error)
}

// GetNotificationServiceCommentList 查询通知-好评有礼
func (dao *NotificationDao) GetNotificationServiceCommentList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationCommentListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.id,psn.member_id,psn.place_id,psn.user_id,member_phone,psn.place_name, place_room,device_sn,psn.type,psn.status, 
	info,us.name, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %k:%i')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %k:%i')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %k:%i')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %k:%i')update_time,
	tc.status c_status,tc.id c_id,tc.img_list,tc.goods_id,tc.app_name,tc.goods_order_id,tc.third_order,g.type goods_type`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")
	db.Joins("LEFT JOIN third_comment tc ON tc.notification_id=psn.id")
	db.Joins("LEFT JOIN goods g ON tc.goods_id=g.id")

	if !helper.IsAdminAndStaff(param.IsAdmin, param.ParentId) {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status == 11 {
		db.Where("tc.status=?", 2)
	} else if param.Status == 12 {
		db.Where("tc.status=?", 3)
	} else if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}
	if param.StaffId > 0 {
		db.Where("psn.user_id=?", param.StaffId)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.Room != "" {
		db.Where("psn.place_room LIKE ?", fmt.Sprintf(`%s%s%s`, "%", param.Room, "%"))
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetNotificationServiceListByStaffId 根据员工ID 查询通知列表
func (dao *NotificationDao) GetNotificationServiceListByStaffId(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.id, member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %T')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %T')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places_staff ps ON psn.place_id=ps.place_id")
	db.Where("ps.staff_id=? AND ps.status in (?)", param.LoginUserId, []int{1, 2})

	if param.Status > 4 {
		db.Where("psn.status=?", param.Status)
	}
	if param.Status <= 4 && param.Status > 0 {
		db.Where("psn.status<=?", param.Status)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetH5NotificationServiceListByStaffId 根据员工ID 查询通知列表(H5根据消息类型查询)
func (dao *NotificationDao) GetH5NotificationServiceListByStaffId(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	staffData, types, _ := dao.GetStaffInfoById(param.LoginUserId)
	if staffData.Id == 0 {
		return dingo.ErrorIf(nil, lst, count)
	}

	fields := `psn.id, member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %T')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %T')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places_staff ps ON psn.place_id=ps.place_id")
	db.Where("ps.staff_id=? AND ps.status in (?)", param.LoginUserId, []int{1, 2})

	if param.PlaceId > 0 {
		db.Where("psn.place_id = ?", param.PlaceId)
	}
	if len(types) > 0 {
		db.Where("psn.type IN ?", types)
	}
	if param.Status > 4 {
		db.Where("psn.status=?", param.Status)
	}
	if param.Status <= 4 && param.Status > 0 {
		db.Where("psn.status<=?", param.Status)
	}
	if param.Type > 0 {
		db.Where("psn.type=?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetNotificationServiceDetailsByStaffId 根据员工ID 查询通知详情
func (dao *NotificationDao) GetNotificationServiceDetailsByStaffId(param *request.StewardGetDetailsReq) *dingo.ErrorWrap {
	data := &request.NotificationListRes{}

	fields := `psn.id, member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %T')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %T')send_time,psn.staff_phone,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places_staff ps ON psn.place_id=ps.place_id")
	db.Where("ps.staff_id=? AND ps.status in (?) AND psn.id=?", param.LoginUserId, []int{1, 2}, param.NotificationId)

	db.Find(&data)
	return dingo.ErrorIf(db.Error, data)
}

func (dao *NotificationDao) SetNotificationServiceDoneByAutomateGoodsBindId(goodsBindId int) *dingo.ErrorWrap {
	sql := `UPDATE places_service_notification psn LEFT JOIN automate_goods_bind agb ON psn.device_sn=agb.device_sn SET 
	psn.status=6 WHERE psn.status<6 AND psn.info LIKE CONCAT('%"routesId":', agb.routes_id, '%') AND agb.id=?`
	db := global.Orm.Exec(sql, goodsBindId)
	return dingo.ErrorIf(db.Error)
}

func (dao *NotificationDao) SetNotificationServiceData(param *request.StewardSetDataReq) *dingo.ErrorWrap {
	sqlData := map[string]interface{}{
		"user_id": param.LoginUserId,
	}
	if param.Status > 0 {
		sqlData["status"] = param.Status
	}
	if param.ServiceMessage != "" {
		sqlData["service_message"] = param.ServiceMessage
	}
	if param.StaffPhone != "" {
		sqlData["staff_phone"] = param.StaffPhone
	}

	db := global.Orm.Table("places_service_notification").Where("id=?", param.NotificationId).Updates(sqlData)
	if db.Error != nil {
		return dingo.ErrorIf(db.Error)
	}

	if (param.Status == 6 || param.Status == 7) && param.CommentId > 0 {
		db.Error = NewCommentDao().SetCommentStatus(&comment.ThirdComment{
			Status:         param.CommentStatus,
			Id:             param.CommentId,
			NotificationId: param.NotificationId,
			GoodsOrderId:   param.GoodsOrderId,
			RejectMessage:  param.CommentMessage,
		}).Err
	}

	return dingo.ErrorIf(db.Error)
}

// 查询用户的服务列表
func (dao *NotificationDao) GetUserNotificationServiceList(param *request.NotificationListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.NotificationListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `psn.id, member_id, place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info,us.name, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %k:%i')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %k:%i')send_time,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %k:%i')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %k:%i')update_time`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN places p ON psn.place_id = p.id")
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")
	db.Where("psn.member_id = ? AND psn.type<>?", param.MemberId, helper.TypeNumNotificationTypeGoods)

	if param.Status > 5 {
		db.Where("psn.status=?", param.Status-1)
	}
	if param.Status <= 5 && param.Status > 0 {
		db.Where("psn.status<=?", 4)
	}

	if param.Type > 0 {
		db.Where("psn.type = ?", param.Type)
	}
	if param.StartTime != "" {
		db.Where("psn.send_time>=?", param.StartTime)
	}
	if param.EndTime != "" {
		db.Where("psn.send_time<?", param.EndTime)
	}
	if param.StaffId > 0 {
		db.Where("psn.user_id=?", param.StaffId)
	}
	if param.PlaceId > 0 {
		db.Where("psn.place_id=?", param.PlaceId)
	}
	if param.Room != "" {
		db.Where("psn.place_room LIKE ?", fmt.Sprintf(`%s%s%s`, "%", param.Room, "%"))
	}

	db.Order("psn.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// 查看服务详情
func (dao *NotificationDao) GetUserNotificationServiceInfo(param *request.NotificationListReq) *dingo.ErrorWrap {
	dataNumInfo := &request.NotificationDetailsRes{}

	fields := `psn.id, member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %T')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %T')send_time,us.role_name,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time,us.name`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")
	//db.Joins("LEFT JOIN roles r ON r.id=us.role_id")
	db.Where("psn.id = ? AND psn.member_id=?", param.Id, param.MemberId)

	db.Find(&dataNumInfo)
	return dingo.ErrorIf(db.Error, dataNumInfo)
}

// 查看服务详情
func (dao *NotificationDao) GetUserNotificationServiceRoomInfo(param *request.NotificationListReq) *dingo.ErrorWrap {
	dataNumInfo := &request.NotificationRoomDetailsRes{}

	fields := `psn.id, member_id, psn.place_id, psn.user_id, member_phone, psn.place_name, place_room, device_sn, type, psn.status, 
	info, psn.message, DATE_FORMAT(make_time,'%Y-%m-%d %T')make_time,DATE_FORMAT(send_time,'%Y-%m-%d %T')send_time,us.role_name,
	DATE_FORMAT(psn.create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(psn.update_time,'%Y-%m-%d %T')update_time,us.name`
	db := global.Orm.Table("places_service_notification psn").Select(fields)
	db.Joins("LEFT JOIN users_staff us ON us.id=psn.user_id")
	//db.Joins("LEFT JOIN roles r ON r.id=us.role_id")
	db.Where("psn.id = ? AND psn.member_id=?", param.Id, param.MemberId)

	db.Find(&dataNumInfo)
	return dingo.ErrorIf(db.Error, dataNumInfo)
}

//工单位列表修改
func (dao *NotificationDao) UpdateUserNotificationServiceInfo(m *placemodel.PlacesServiceNotification) *dingo.ErrorWrap {
	db := global.Orm.Table("places_service_notification").Where("id = ?", m.Id).Model(&m).Updates(&m)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *NotificationDao) SetPlaceServiceConfig(param *request.SetStewardPlaceSetServiceConfigReq) *dingo.ErrorWrap {
	sqlData := map[string]interface{}{
		"service_config": param.Config,
	}

	db := global.Orm.Table("places").Where("id=?", param.PlaceId).Updates(sqlData)
	return dingo.ErrorIf(db.Error)
}

func (dao *NotificationDao) GetPlaceServiceConfig(param *request.SetStewardPlaceSetServiceConfigReq) *dingo.ErrorWrap {
	var config string

	db := global.Orm.Table("places").Select("service_config").Where("id=?", param.PlaceId).Find(&config)
	return dingo.ErrorIf(db.Error, config)
}

// 查询好评详情
func (dao *NotificationDao) GetCommentDetails(param *request.CommentInfoReq) *dingo.ErrorWrap {
	dataInfo := &request.CommentDetailsInfoRes{}
	fields := `concat(ad.province_name,ad.city_name,ad.county_name,ad.address) address,ad.phone,ad.name,tc.goods_id,g.type goods_type,
	g.status,g.type,g.price,g.discount_amount,g.coupon_id,g.icon,g.img_list,g.brand_name,g.name goods_name,p.place_name,tc.id,
	p.address place_address,p.province,p.city,p.country,tc.img_list comment_img_list,tc.message,tc.goods_order_id,tc.status,tc.reject_message`
	db := global.Orm.Table("third_comment tc").Select(fields)
	db.Joins("LEFT JOIN goods g ON g.id = tc.goods_id ")
	db.Joins("LEFT JOIN goods_orders gr ON gr.id = tc.goods_order_id ")
	db.Joins("LEFT JOIN member_address ad ON ad.id =gr.address_id")
	db.Joins("LEFT JOIN places p ON p.id = tc.place_id ")
	db.Where("tc.id = ? AND tc.member_id=?", param.Id, param.MemberId)

	db.Find(&dataInfo)
	return dingo.ErrorIf(db.Error, dataInfo)
}

func (dao *NotificationDao) GetH5CommentDetails(param *request.CommentInfoReq) *dingo.ErrorWrap {
	dataInfo := &request.CommentDetailsInfoRes{}
	fields := `ad.address,ad.phone,ad.name,tc.goods_id,g.status,g.type,g.price,g.discount_amount,g.coupon_id,g.icon,
	g.img_list,g.brand_name,g.name goods_name,p.place_name,p.address place_address,p.province,p.city,p.country,
	tc.img_list comment_img_list,tc.message,tc.goods_order_id,tc.status,tc.reject_message`
	db := global.Orm.Table("third_comment tc").Select(fields)
	db.Joins("LEFT JOIN member_address ad ON ad.member_id =tc.member_id")
	db.Joins("LEFT JOIN goods g ON g.id = tc.goods_id ")
	db.Joins("LEFT JOIN places p ON p.id = tc.place_id ")
	db.Where("tc.notification_id = ?", param.NotificationId)

	db.Find(&dataInfo)
	return dingo.ErrorIf(db.Error, dataInfo)
}

// 更新红包信息
func (dao *NotificationDao) UpdateRedPacketReceiveInfo(param *comment.SendRedPacketRecord) *dingo.ErrorWrap {
	dbData := map[string]interface{}{}
	if param.ReceiveInfo != "" {
		dbData["receive_info"] = param.ReceiveInfo
	}
	if param.Status == helper.TypeNumRedPacketStatusAlready {
		dbData["receive_time"] = time.Now().Format("2006-01-02 15:04:05")
		dbData["status"] = param.Status
	}
	if param.Status == helper.TypeNumRedPacketStatusPast {
		dbData["status"] = param.Status
	}

	db := global.Orm.Model(&param).Where("id=?", param.Id)

	if param.MemberId > 0 {
		db.Where("member_id=?", param.MemberId)
	}

	db.Updates(dbData)
	return dingo.ErrorIf(db.Error)
}

// 查询好红包领取状态
func (dao *NotificationDao) GetCommentRedPacketReceiveInfo(param *request.ThirdCommentSubmitReq) *dingo.ErrorWrap {
	dbInfo := &comment.SendRedPacketRecord{}
	db := global.Orm.Table("send_red_packet_record sr").Select("sr.*")
	db.Joins("LEFT JOIN third_comment tc ON sr.order_id=tc.goods_order_id")
	db.Where("notification_id=? AND sr.member_id=?", param.NotificationId, param.MemberId)
	//db.Where("sr.status=?", helper.TypeNumRedPacketStatusNotAlready)
	db.Find(&dbInfo)
	return dingo.ErrorIf(db.Error, dbInfo)
}

// 查询好评可领取红包
func (dao *NotificationDao) GetCommentRedPacketReceiveListByOrderIds(orderIds []int) *dingo.ErrorWrap {
	var dbInfo []*comment.SendRedPacketRecord
	db := global.Orm.Table("send_red_packet_record sr")
	db.Where("sr.order_id IN ?", orderIds)
	db.Find(&dbInfo)
	return dingo.ErrorIf(db.Error, dbInfo)
}

func (dao *NotificationDao) CommentSubmit(m *request.ThirdCommentSubmitReq) *dingo.ErrorWrap {

	db := global.Orm.Table("third_comment").Create(&m)
	return dingo.ErrorIf(db.Error, m)
}

func (dao *NotificationDao) SetCommentGoods(m *request.ThirdCommentSubmitReq) *dingo.ErrorWrap {
	sql := `UPDATE third_comment tc LEFT JOIN goods_orders go ON tc.goods_order_id=go.id 
	SET tc.goods_id=?,go.goods_id=? WHERE tc.id=? AND tc.member_id=? AND go.id>0`
	db := global.Orm.Exec(sql, m.GoodsId, m.GoodsId, m.Id, m.MemberId)
	return dingo.ErrorIf(db.Error)
}

func (dao *NotificationDao) GetUserAddress(memberId int) *dingo.ErrorWrap {
	var list []*membermodel.MemberAddress
	db := global.Orm.Table("member_address").Where("member_id = ?", memberId).Find(&list)
	return dingo.ErrorIf(db.Error, list)
}

func (dao *NotificationDao) RemoveUserAddress(m *membermodel.MemberAddress) *dingo.ErrorWrap {
	db := global.Orm.Model(&m).Where("member_id=?", m.MemberId).Delete("id", m.Id)
	return dingo.ErrorIf(db.Error, m)
}

func (dao *NotificationDao) UpdateUserAddress(m *membermodel.MemberAddress) *dingo.ErrorWrap {
	db := global.Orm.Table("member_address").Where("id = ?", m.Id).Model(&m).Updates(&m)
	return dingo.ErrorIf(db.Error, m)
}

func (dao *NotificationDao) CreateUserAddress(m *membermodel.MemberAddress) *dingo.ErrorWrap {

	db := global.Orm.Table("member_address").Create(&m)
	return dingo.ErrorIf(db.Error, m)
}

//查询地址详情
func (dao *NotificationDao) GetUserAddressDetails(m *membermodel.MemberAddress) *dingo.ErrorWrap {
	addressInfo := membermodel.MemberAddress{}
	db := global.Orm.Table("member_address").Where("member_id = ? and id = ?", m.MemberId, m.Id).Find(&addressInfo)
	return dingo.ErrorIf(db.Error, addressInfo)
}

func (dao *NotificationDao) CreateCommentNotificationService(param *request.ThirdCommentSubmitReq) *dingo.ErrorWrap {
	m := &placeresp.NotificationUpdate{}
	m.MemberId = param.MemberId
	m.PlaceId = param.PlaceId
	m.MemberPhone = param.Phone
	m.PlaceName = param.PlaceName
	m.PlaceRoom = param.PlaceRoom
	m.DeviceSn = param.DeviceSn
	m.NotificationType = 9
	m.Status = 4
	m.Info = "好评有礼"

	db := global.Orm.Table("places_service_notification").Create(&m)
	return dingo.ErrorIf(db.Error, m)
}

// 好评审核通知确认信息
func (dao *NotificationDao) GetCommentNotificationByCommentId(commentId int) *dingo.ErrorWrap {
	info := &comment.ThirdCommentInfoWithAddressAndService{}
	fields := `tc.place_id,tc.place_name,psn.place_room,psn.device_sn,tc.app_name,tc.third_order,g.icon,g.name as goods_name,
tc.message,m.phone,m.name,m.postcode,m.province_name,m.city_name,m.county_name,m.address,tc.create_time,tc.img_list`
	db := global.Orm.Table("third_comment tc").Select(fields).
		Joins("LEFT JOIN places_service_notification psn ON tc.notification_id = psn.id").
		Joins("LEFT JOIN goods g ON tc.goods_id = g.id").
		Joins("LEFT JOIN goods_orders go ON tc.goods_order_id = go.id").
		Joins("LEFT JOIN member_address m ON tc.member_id = m.member_id")
	db.Where("tc.id = ?", commentId)
	db.Find(&info)
	return dingo.ErrorIf(db.Error, info)
}
